from browser . gui import browser_gui

class frame_fake :
    def __init__ ( self , master ) :
        self . _master = master
        self . _destroyed = False
        self . _packed = False
        self . _focused = False
        
class label_fake :
    def __init__ ( self , master , text ) :
        self . _master = master
        self . _text = text

class button_fake :
    def __init__ ( self , master , text , command ) :
        self . _master = master
        self . _text = text
        self . _command = command

class entry_fake :
    def __init__ ( self ) :
        self . _text = None
        self . _focused = False

class focus_in_event_fake :
    _deleted_count = 0
    def __init__ ( self , widget , handler ) :
        self . _widget = widget
        self . _handler = handler
    def __del__ ( self ) :
        focus_in_event_fake . _deleted_count += 1

class focus_out_event_fake :
    _deleted_count = 0
    def __init__ ( self , widget , handler ) :
        self . _widget = widget
        self . _handler = handler
    def __del__ ( self ) :
        focus_out_event_fake . _deleted_count += 1

class mediator_fake :
    def __init__ ( self ) :
        self . _browser_gui = None
        self . _application_window = None
        self . _widgets = [ ]
        self . _buttons = [ ]
        self . _frames = [ ]
        self . _labels = [ ]
        self . _table_entries = [ ]
        self . _new_table_in_frames = [ ]
        self . _browser_return = None
        self . _table_history = [ ]
        self . _focus_in_events = [ ]
        self . _focus_out_events = [ ]
        self . _entry_text_in_browser_reply = None
    def application_window_request ( self ) :
        self . _browser_gui . application_window_reply ( self . _application_window )
    def gui_destroy_frame ( self , frame ) :
        frame . _destroyed = True
    def gui_create_frame_request ( self , master ) :
        frame = frame_fake ( master )
        self . _widgets += [ frame ]
        self . _frames += [ frame ]
        self . _browser_gui . gui_create_frame_reply ( frame )
    def gui_create_label_request ( self , master , text ) :
        label = label_fake ( master , text )
        self . _widgets += [ label ]
        self . _labels += [ label ]
        self . _browser_gui . gui_create_label_reply ( label )
    def gui_create_button_request ( self , master , text , command ) :
        button = button_fake ( master , text , command )
        self . _widgets += [ button ]
        self . _buttons += [ button ]
        self . _browser_gui . gui_create_button_reply ( button )
    def gui_entry_text_request ( self , entry ) :
        self . _browser_gui . gui_entry_text_reply ( entry . _text )
    def gui_set_entry_text ( self , entry , text ) :
        entry . _text = text
    def gui_set_label_text ( self , label , text ) :
        label . _text = text
    def gui_pack ( self , widget , padx = None , pady = None , side = None , fill = None ) :
        widget . _packed = True
    def gui_side_top ( self ) :
        pass
    def gui_fill_x ( self ) :
        pass
    def gui_anchor_w ( self ) :
        pass
    def gui_anchor_e ( self ) :
        pass
    def create_new_table_in_frame ( self , frame ) :
        self . _new_table_in_frames += [ frame ]
    def localization_browser_return ( self ) :
        return self . _browser_return
    def on_browser_return ( self ) :
        pass
    def on_browser_action_gui_button ( self ) :
        pass
    def add_table_label ( self , text = None , anchor = None , borderwidth = None ) :
        self . _table_history += [ [ mediator_fake . add_table_label , text ] ]
    def add_table_entry_request ( self ) :
        new_entry = entry_fake ( )
        self . _table_history += [ [ mediator_fake . add_table_entry_request ] ]
        self . _table_entries += [ new_entry ]
        self . _browser_gui . add_table_entry_reply ( new_entry )
    def finish_table_row ( self ) :
        self . _table_history += [ [ mediator_fake . finish_table_row ] ]
    def gui_move_focus_to ( self , widget ) :
        widget . _focused = True
    def on_browser_entry_focus ( self ) :
        pass
    def on_browser_entry_unfocus ( self ) :
        pass
    def gui_focus_in_event_request ( self , widget , handler ) :
        event = focus_in_event_fake ( widget , handler )
        self . _focus_in_events += [ event ]
        self . _browser_gui . gui_focus_in_event_reply ( event )
    def gui_focus_out_event_request ( self , widget , handler ) :
        event = focus_out_event_fake ( widget , handler )
        self . _focus_out_events += [ event ]
        self . _browser_gui . gui_focus_out_event_reply ( event )
    def entry_text_in_browser_reply ( self , text ) :
        self . _entry_text_in_browser_reply = text

class browser_gui_tests :
    'browser_gui'
    def __init__ ( self , test ) :
        self . _test = test
        self . _mediator = mediator_fake ( )
        self . _browser_gui = browser_gui ( self . _mediator )
        self . _mediator . _browser_gui = self . _browser_gui
        focus_in_event_fake . _deleted_count = 0
        focus_out_event_fake . _deleted_count = 0
    def test_show_browser_create_frame ( self ) :
        self . _mediator . _application_window = 'my window'
        self . _browser_gui . show_browser ( )
        frame = self . _mediator . _widgets [ 0 ]
        self . _test . assume_equal ( frame . _master , 'my window' )
        self . _test . assume_true ( frame . _packed ) 
    def test_hide_browser_destroy_frame ( self ) :
        self . _browser_gui . show_browser ( )
        self . _browser_gui . hide_browser ( )
        frame = self . _mediator . _widgets [ 0 ]
        self . _test . assume_true ( frame . _destroyed )
    def test_add_title_row_to_browser ( self ) :
        self . _browser_gui . show_browser ( )
        self . _browser_gui . add_title_row_to_browser \
            ( number = 'my number' 
            , name = 'my name'
            , creation_time = 'my creation time'
            , modification_time = 'my modification time'
            , size = 'my size'
            )
        self . _test . assume_equal ( self . _mediator . _table_history , 
            [ [ mediator_fake . add_table_label , 'my number' ]
            , [ mediator_fake . add_table_label , 'my name' ]
            , [ mediator_fake . add_table_label , 'my creation time' ]
            , [ mediator_fake . add_table_label , 'my modification time' ]
            , [ mediator_fake . add_table_label , 'my size' ]
            , [ mediator_fake . finish_table_row ]
            ] )
    def test_add_existing_row_to_browser ( self ) :
        self . _browser_gui . show_browser ( )
        self . _browser_gui . add_existing_row_to_browser \
            ( number = 'my number'
            , name = 'my name'
            , creation_time = 'my creation time'
            , modification_time = 'my modification time'
            , size = 'my size'
            )
        self . _test . assume_equal ( self . _mediator . _table_history , 
            [ [ mediator_fake . add_table_label , 'my number' ]
            , [ mediator_fake . add_table_label , 'my name' ]
            , [ mediator_fake . add_table_label , 'my creation time' ]
            , [ mediator_fake . add_table_label , 'my modification time' ]
            , [ mediator_fake . add_table_label , 'my size' ]
            , [ mediator_fake . finish_table_row ]
            ] )
    def test_add_empty_row_to_browser ( self ) :
        self . _browser_gui . show_browser ( )
        self . _browser_gui . add_empty_row_to_browser ( )
        self . _test . assume_equal ( self . _mediator . _table_history , 
            [ [ mediator_fake . add_table_label , None ]
            , [ mediator_fake . add_table_label , None ]
            , [ mediator_fake . add_table_label , None ]
            , [ mediator_fake . add_table_label , None ]
            , [ mediator_fake . add_table_label , None ]
            , [ mediator_fake . finish_table_row ]
            ] )
    def test_add_action_row_to_browser ( self ) :
        self . _browser_gui . show_browser ( )
        self . _browser_gui . add_action_row_to_browser ( )
        self . _test . assume_equal ( self . _mediator . _table_history , 
            [ [ mediator_fake . add_table_label , None ]
            , [ mediator_fake . add_table_entry_request ]
            , [ mediator_fake . add_table_label , None ]
            , [ mediator_fake . add_table_label , None ]
            , [ mediator_fake . add_table_label , None ]
            , [ mediator_fake . finish_table_row ]
            ] )
    def test_add_action_row_to_browser_entry_focus_event ( self ) :
        self . _browser_gui . show_browser ( )
        self . _browser_gui . add_action_row_to_browser ( )
        event = self . _mediator . _focus_in_events [ 0 ]
        entry = self . _mediator . _table_entries [ 0 ]
        self . _test . assume_equal ( event . _widget , entry )
        self . _test . assume_equal ( event . _handler , self . _mediator . on_browser_entry_focus )
    def test_add_action_row_to_browser_entry_focus_event_persistance ( self ) :
        self . _browser_gui . show_browser ( )
        self . _browser_gui . add_action_row_to_browser ( )
        self . _mediator . _focus_in_events = [ ]
        self . _test . assume_equal ( focus_in_event_fake . _deleted_count , 0 )
    def test_add_action_row_to_browser_entry_unfocus_event ( self ) :
        self . _browser_gui . show_browser ( )
        self . _browser_gui . add_action_row_to_browser ( )
        event = self . _mediator . _focus_out_events [ 0 ]
        entry = self . _mediator . _table_entries [ 0 ]
        self . _test . assume_equal ( event . _widget , entry )
        self . _test . assume_equal ( event . _handler , self . _mediator . on_browser_entry_unfocus )
    def test_add_action_row_to_browser_entry_unfocus_event_persistance ( self ) :
        self . _browser_gui . show_browser ( )
        self . _browser_gui . add_action_row_to_browser ( )
        self . _mediator . _focus_out_events = [ ]
        self . _test . assume_equal ( focus_out_event_fake . _deleted_count , 0 )
    def test_focus_entry_in_creation_menu ( self ) :
        self . _browser_gui . show_browser ( )
        self . _browser_gui . add_action_row_to_browser ( )
        self . _browser_gui . focus_entry_in_browser ( )
        entry = self . _mediator . _table_entries [ 0 ]
        self . _test . assume_true ( entry . _focused )
    def test_unfocus_entry_in_browser ( self ) :
        self . _browser_gui . show_browser ( )
        self . _browser_gui . unfocus_entry_in_browser ( )
        frame = self . _mediator . _widgets [ 0 ]
        self . _test . assume_true ( frame . _focused )
    def test_set_entry_text_in_browser ( self ) :
        self . _browser_gui . show_browser ( )
        self . _browser_gui . add_action_row_to_browser ( )
        self . _browser_gui . set_entry_text_in_browser ( 'my text' )
        entry = self . _mediator . _table_entries [ 0 ]
        self . _test . assume_equal ( entry . _text , 'my text' )
    def test_set_title_text_in_browser ( self ) :
        self . _browser_gui . show_browser ( )
        self . _browser_gui . add_title_label_to_browser ( None )
        self . _browser_gui . set_title_text_in_browser ( 'my title' )
        label = self . _mediator . _widgets [ 1 ]
        self . _test . assume_equal ( label . _text , 'my title' )
    def test_entry_text_in_browser ( self ) :
        self . _browser_gui . show_browser ( )
        self . _browser_gui . add_action_row_to_browser ( )
        self . _browser_gui . set_entry_text_in_browser ( 'my text' )
        self . _browser_gui . entry_text_in_browser_request ( )
        self . _test . assume_equal ( self . _mediator . _entry_text_in_browser_reply , 'my text' )
    def test_add_action_button_to_browser ( self ) :
        self . _browser_gui . show_browser ( )
        self . _mediator . _buttons = [ ]
        self . _browser_gui . add_action_button_to_browser ( 'my action' )
        frame = self . _mediator . _frames [ 0 ]
        button = self . _mediator . _buttons [ 0 ]
        self . _test . assume_equal ( button . _master , frame )
        self . _test . assume_equal ( button . _text , 'my action' )
        self . _test . assume_equal ( button . _command , self . _mediator . on_browser_action_gui_button )
        self . _test . assume_true ( button . _packed )
    def test_add_return_button_to_browser ( self ) :
        self . _mediator . _browser_return = 'my return'
        self . _browser_gui . show_browser ( )
        self . _mediator . _buttons = [ ]
        self . _browser_gui . add_return_button_to_browser ( )
        frame = self . _mediator . _frames [ 0 ]
        self . _test . assume_equal ( [ button . _master for button in self . _mediator . _buttons ] , [ frame ] )
        self . _test . assume_equal ( [ button . _text for button in self . _mediator . _buttons ] , [ 'my return' ] )
        self . _test . assume_equal ( [ button . _command for button in self . _mediator . _buttons ] , [ self . _mediator . on_browser_return ] )
        self . _test . assume_equal ( [ button . _packed for button in self . _mediator . _buttons ] , [ True ] )
    def test_add_title_label_to_browser ( self ) :
        self . _browser_gui . show_browser ( )
        self . _mediator . _labels = [ ]
        self . _browser_gui . add_title_label_to_browser ( 'my title' )
        frame = self . _mediator . _frames [ 0 ]
        self . _test . assume_equal ( [ label . _master for label in self . _mediator . _labels ] , [ frame ] )
        self . _test . assume_equal ( [ label . _text for label in self . _mediator . _labels ] , [ 'my title' ] )
        self . _test . assume_equal ( [ label . _packed for label in self . _mediator . _labels ] , [ True ] )
    def test_add_table_to_browser_add_table_frame ( self ) :
        self . _browser_gui . show_browser ( )
        master_frame = self . _mediator . _frames [ 0 ]
        self . _mediator . _frames = [ ]
        self . _browser_gui . add_table_to_browser ( )
        self . _test . assume_equal ( [ frame . _master for frame in self . _mediator . _frames ] , [ master_frame ] )
        self . _test . assume_equal ( [ frame . _packed for frame in self . _mediator . _frames ] , [ True ] )
    def test_add_table_to_browser_create_new_table_in_frame ( self ) :
        self . _browser_gui . show_browser ( )
        master_frame = self . _mediator . _frames [ 0 ]
        self . _mediator . _frames = [ ]
        self . _mediator . _new_table_in_frames = [ ]
        self . _browser_gui . add_table_to_browser ( )
        table_frame = self . _mediator . _frames [ 0 ]
        self . _test . assume_equal ( self . _mediator . _new_table_in_frames , [ table_frame ] )
